package com.eastfair.projectcore.service.impl;

import com.eastfair.cache.projectcore.DictionaryCacheKeyBuilder;
import com.eastfair.core.model.CacheHashKey;
import com.eastfair.projectcore.dao.DictionaryTypeMapper;
import com.eastfair.projectcore.entity.Dictionary;
import com.eastfair.projectcore.entity.DictionaryType;
import com.eastfair.projectcore.service.DictionaryService;
import com.eastfair.projectcore.service.DictionaryTypeService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.uid.baidu.rule.SnowFlakeRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;
import java.util.List;

//import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2022-07-14
 */
@Slf4j
@Service
public class DictionaryTypeServiceImpl extends SuperServiceImpl<DictionaryTypeMapper, DictionaryType> implements DictionaryTypeService {
    @Autowired
    DictionaryService dictionaryService;
    @Autowired
    SnowFlakeRole snowFlakeRole;

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(DictionaryType model) {
        model.setId(snowFlakeRole.findUidSimple());
        return R.successDef();
    }


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<DictionaryType> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
            //删除字典信息
            Dictionary dictionary = new Dictionary();
            dictionary.setTypeId(s.getId());
            List<Dictionary> dictionaryList = dictionaryService.list(dictionary, null);
            if (dictionaryList != null && !dictionaryList.isEmpty()) {
                for (Dictionary dictionary1 : dictionaryList) {
                    dictionary1.setIsDeleted(BusinessConstant.YES);
                    dictionaryService.updateById(dictionary1);
                }
            }
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等

        return R.successDef();
    }


    /**
     * 处理修改之后的相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerUpdateByIdBack(DictionaryType model) {
        Dictionary dictionary = new Dictionary();
        dictionary.setTypeId(model.getId());
        List<Dictionary> dictionaryList = dictionaryService.list(dictionary, null);
        if (dictionaryList != null && !dictionaryList.isEmpty()) {
            for (Dictionary dictionary1 : dictionaryList) {
                dictionary1.setType(model.getType());
                dictionary1.setLabel(model.getLabel());
                dictionary1.setIsDeleted(model.getIsDeleted());
                dictionaryService.updateById(dictionary1);
            }
        }
        return R.successDef();
    }

    /**
     * 处理新增之后的相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSaveBack(DictionaryType model) {
        model.setId(snowFlakeRole.findUidSimple());
        return R.successDef();
    }

}
